package com.tpc.client.systeme.core;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FetchMode;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.TabSet;
import com.tpc.client.modules.core.contact.ContactUI;
import com.tpc.client.modules.core.dashboard.DashboardUi;
import com.tpc.client.modules.core.organisation.OrganisationUI;
import com.tpc.client.systeme.core.datasource.CityDataSource;
import com.tpc.client.systeme.core.datasource.ContactDataSource;
import com.tpc.client.systeme.core.datasource.CountryDataSource;
import com.tpc.client.systeme.core.datasource.OrganisationCategoryDataSource;
import com.tpc.client.systeme.core.datasource.OrganisationDataSource;
import com.tpc.client.systeme.core.datasource.OrganisationTypeDataSource;
import com.tpc.client.systeme.core.datasource.RessourceDataSource;
import com.tpc.client.systeme.core.datasource.StateDataSource;
import com.tpc.client.systeme.core.datasource.ZipCodeDataSource;
import com.tpc.client.systeme.core.error.TPCauthentificationException;
import com.tpc.client.systeme.core.i18n.Dictionary;

/**
 * @version 0.0.1
 * @author frederic.fortin
 * @modified 2011-02-07
 */
public class Systeme {

    private String pageUrl;
    private TabSet AppBar;
    private DashboardUi dashboardUI;
    private OrganisationUI organisationUI;
    private ContactUI contactUI;
    private String previousPage;
    private ListGrid previousGrid;
    private String previousObjectTerm;
    private String previousObjectId;
    private String userId;
    private Boolean notLoadFromListe;
    private Img imgLoading;
    private int nbrModule = 0;
    private int nbrModuleToHideSplash = 0;
    private int currentCountToHideSplash = 1;
    
    private ZipCodeDataSource ZIPDS = null;
    private CityDataSource CITYDS = new CityDataSource("CITYDS");
    private StateDataSource STATEDS = new StateDataSource("STATEDS");
    private CountryDataSource COUNTRYDS = new CountryDataSource("COUNTRYDS");
    private OrganisationDataSource ORGANISATIONDS = new OrganisationDataSource("ORGANISATION_DS");
    private ContactDataSource CONTACTDS = new ContactDataSource("CONTACTDS");
    private RessourceDataSource RESSOURCEDS = new RessourceDataSource("RESSOURCEDS");
    private RessourceDataSource MANAGERDS = new RessourceDataSource("MANAGERDS");
    private OrganisationCategoryDataSource ORG_CAT_DS = new OrganisationCategoryDataSource("ORG_CAT_DS");
    private OrganisationTypeDataSource ORG_TYP_DS = new OrganisationTypeDataSource("ORG_TYP_DS");
    
    private String user_organisationid = null;
    private String user_username = null;
    private String user_firstname = null;
    private String user_lastname = null;
    private String user_dictionary = null;
    private String user_group = null;
    private Boolean user_isAdmin = false;
    private String user_organisationname = null;
    
    private Dictionary dictionary = null;

    private Overflow FormOverflow = Overflow.HIDDEN;
    
    private VLayout vltMainPage = new VLayout();
    private LayoutSpacer leftPadding = new LayoutSpacer();
    private LayoutSpacer rightPadding = new LayoutSpacer();

    private String mainPageWidth;
    
    private String TabPaneWidth;
    private String TabPaneHeight;
    private int TabPaneMargin;
    private String TabPaneBGcolor;
    private Overflow TabPaneOverFlow;

    private String GridWidth;
    private String GridHeight;
    private String GridBGcolor;
    private Boolean GridAlternateRecord;
    private String GridEmptyMessage;
    private Boolean GridShowEmptyMsg;
    private String GridLoadingMsg;
    private Boolean GridShowEditor;
    private Boolean GridAutoFetchData;
    private FetchMode GridDataFetchMode;

    private String FormWidth;
    private String FormHeight;
    private String FormBGcolor;
    private int FormCellpadding;
    private Alignment FormAlign;
    private TitleOrientation FormTitleOrientation;
    private String FormTitleSuffixe;
    private Boolean FormHiliteRequired;
    private String FormRTitlePrefix;
    private String FormRTitleSuffix;
    private Boolean FormAutoFetch;

    private String HomePanelWidth;
    private String HomePanelHeight;
    private int HomePanelShadowDepth;
    private Boolean HomePanelShowShadow;
    private String HomePanelBGcolor;

    public Systeme() {
        userId = Cookies.getCookie("krypton_userid");
        user_username = Cookies.getCookie("krypton_username");
        user_firstname = Cookies.getCookie("krypton_firstname");
        user_lastname = Cookies.getCookie("krypton_lastname");
        user_dictionary = Cookies.getCookie("krypton_dictionary");
        user_group = Cookies.getCookie("krypton_group");
        user_isAdmin = new Boolean(Cookies.getCookie("krypton_isAdmin"));
        user_organisationid = Cookies.getCookie("krypton_organisationid");
        user_organisationname = Cookies.getCookie("krypton_organisationname");
        pageUrl = Page.getAppDir();     
        pageUrl = pageUrl.substring(0,pageUrl.length()-1);
        int last_slash = pageUrl.lastIndexOf("/");
        if (last_slash > 7) {
            pageUrl = pageUrl.substring(pageUrl.lastIndexOf("/"));
        }
        ZIPDS = new ZipCodeDataSource("ZIPDS",this);
    }
    
    public void setSplashScreenCurrentModule(String MODULE_TITLE) {
        if (DOM.getElementById("splashScreen") != null) {
            DOM.getElementById("splashScreenText").setInnerHTML(MODULE_TITLE+"...");
        }
    }
    /** NEED TO VALIDATE ALL LIST LOADED BEFORE HIDDGIN SPLASH SCREEB **/
    public void hideSplashScreen() {
        if (currentCountToHideSplash < getnbrModuleToHideSplash()) {
            currentCountToHideSplash++;
        }
        else {
            if (DOM.getElementById("splashScreen") != null) {
                DOM.removeChild(RootPanel.getBodyElement(),DOM.getElementById("splashScreen"));
            }
        }
    }

    public void handleServerException(Throwable exception) {
        if (exception instanceof TPCauthentificationException) {
            Window.Location.assign(pageUrl+"/index.jsp?error=session_dead");
        }
        else {
            SC.say("OUPS ! Server error :-( "+exception.getMessage());
        }
    }

    public String getPageUrl() {
        return pageUrl;
    }
    
    public void setDictionary(Dictionary param) {
        dictionary = param;
    }
    public Dictionary getDictionary() {
        return dictionary;
    }
    
    public String getUsername() {
        return user_username;
    }
    public String getLastname() {
        return user_lastname;
    }
    public String getFirstname() {
        return user_firstname;
    }
    public String geDictionary() {
        return user_dictionary;
    }
    public String getUserGroup() {
        return user_group;
    }

    public Boolean isAdmin() {
        return user_isAdmin;
    }

    public String getUserOrganisationId() {
        return user_organisationid;
    }
    
    public String getUserOrganisationName() {
        return user_organisationname;
    }

    public ZipCodeDataSource getZIPDS() {
        return ZIPDS;
    }
    public CityDataSource getCITYDS() {
        return CITYDS;
    }
    public StateDataSource getSTATEDS() {
        return STATEDS;
    }
    public CountryDataSource getCOUNTRYDS() {
        return COUNTRYDS;
    }
    public RessourceDataSource getRESSDS() {
        return RESSOURCEDS;
    }
    public RessourceDataSource getMANAGERDS() {
        return MANAGERDS;
    }
    public OrganisationDataSource getORGANISATIONDS() {
        return ORGANISATIONDS;
    }
    public OrganisationCategoryDataSource getOrganisationCategories(Systeme sysparam) {
    	ORG_CAT_DS.setparams(sysparam);
    	return ORG_CAT_DS;
    }
    public OrganisationTypeDataSource getOrganisationTypes(Systeme sysparam) {
    	ORG_TYP_DS.setparams(sysparam);
    	return ORG_TYP_DS;
    }

    public void setNbrModule(int param) {
        nbrModule = param;
    }
    public int getNbrModule() {
        return nbrModule;
    }
        
    public void setnbrModuleToHideSplash(int[] param) {
        int splashingModule = 0;
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                if (param[i] == 1) {
                    splashingModule++;
                }
            }
        }
        nbrModuleToHideSplash = splashingModule;
    }
    public int getnbrModuleToHideSplash() {
        return nbrModuleToHideSplash;
    }
    
    public void setNotLoadFromList(Boolean param) {
        notLoadFromListe = param;
    }
    public Boolean getNotLoadFromList() {
        return notLoadFromListe;
    }

    public void setLoading(Img param) {
        imgLoading = param;
    }
    public Img getLoading() {
        return imgLoading;
    }

    public void setDashboardUI(DashboardUi param) {
        dashboardUI = param;
    }
    public DashboardUi getDashboardUI() {
        return dashboardUI;
    }
   
    public void setOrganisationUI(OrganisationUI param) {
        organisationUI = param;
    }
    public OrganisationUI getOrganisationUI() {
        return organisationUI;
    }

    public void setContactUI(ContactUI param) {
        contactUI = param;
    }
    public ContactUI getContactUI() {
        return contactUI;
    }
    
    public void setPreviousPage(String param) {
        previousPage = param;
    }
    public String getPreviousPage() {
        return previousPage;
    }

    public void setPreviousGrid(ListGrid param) {
        previousGrid = param;
    }
    public ListGrid getPreviousGrid() {
        return previousGrid;
    }

    public void setPreviousObjectTerm(String param) {
        previousObjectTerm = param;
    }
    public String getPreviousObjectTerm() {
        return previousObjectTerm;
    }

    public void setPreviousObjectId(String param) {
        previousObjectId = param;
    }
    public String getPreviousObjectId() {
        return previousObjectId;
    }

    public void setUserId(String param) {
        userId = param;
    }
    public String getUserId() {
        return userId;
    }

    public void setAppBar(TabSet param) {
        AppBar = param;
    }
    public TabSet getAppBar() {
        return AppBar;
    }
    

    public void stopLoading() {
        imgLoading.hide();
    }

    public void startLoading() {
        imgLoading.show();
    }

    public void setMainPage(VLayout param) {
        vltMainPage = param;
    }
    public VLayout getMainPage() {
        return vltMainPage;
    }
    
    public void setMainPageWidth(String param) {
        mainPageWidth = param;
    }
    public String getMainPageWidth() {
        return mainPageWidth;
    }

    public void setLeftPadding(LayoutSpacer leftSpacer) {
        leftPadding = leftSpacer;
    }
    public LayoutSpacer getLeftPadding() {
        return leftPadding;
    }

    public void setRightPadding(LayoutSpacer rightSpacer) {
        rightPadding = rightSpacer;
    }
    public LayoutSpacer getRightPadding() {
        return rightPadding;
    }

    public void setTabPaneWidth(String param) {
        TabPaneWidth = param;
    }

    public void setTabPaneHeight(String param) {
        TabPaneHeight = param;
    }

    public void setTabPaneMargin(int param) {
        TabPaneMargin = param;
    }

    public void setTabPaneBGcolor(String param) {
        TabPaneBGcolor = param;
    }

    public void setFormOverflow(Overflow param) {
        FormOverflow = param;
    }
    public Overflow getFormOverflow() {
        return FormOverflow;
    }
    
    public void setTabPaneOverflow(Overflow param) {
        TabPaneOverFlow = param;
    }

    public void setGridWidth(String param) {
        GridWidth = param;
    }

    public void setGridHeight(String param) {
        GridHeight = param;
    }

    public void setGridBGcolor(String param) {
        GridBGcolor = param;
    }

    public void setGridAlternate(Boolean param) {
        GridAlternateRecord = param;
    }

    public void setGridEmptyMessage(String param) {
        GridEmptyMessage = param;
    }

    public void setGridShowEmptyMsg(Boolean param) {
        GridShowEmptyMsg = param;
    }

    public void setGridLoadingMsg(String param) {
        GridLoadingMsg = param;
    }

    public void setGridShowFilterEditor(Boolean param) {
        GridShowEditor = param;
    }

    public void setGridAutoFetchData(Boolean param) {
        GridAutoFetchData = param;
    }

    public void setGridDataFetchMode(FetchMode param) {
        GridDataFetchMode = param;
    }

    public void setFormWidth(String param) {
        FormWidth = param;
    }

    public void setFormHeight(String param) {
        FormHeight = param;
    }

    public void setFormBGcolor(String param) {
        FormBGcolor = param;
    }

    public void setFormCellPadding(int param) {
        FormCellpadding = param;
    }

    public void setFormAlign(Alignment param) {
        FormAlign = param;
    }

    public void setFormTitleOrientation(TitleOrientation param) {
        FormTitleOrientation = param;
    }

    public void setFormTitleSuffixe(String param) {
        FormTitleSuffixe = param;
    }

    public void setFormHiliteRequired(Boolean param) {
        FormHiliteRequired = param;
    }

    public void setFormRtitlePrefix(String param) {
        FormRTitlePrefix = param;
    }

    public void setFormRTitleSuffix(String param) {
        FormRTitleSuffix = param;
    }

    public void setFormAutoFetchData(Boolean param) {
        FormAutoFetch = param;
    }

    public String getTabPaneBGcolor() {
        return TabPaneBGcolor;
    }

    public void setHomePanelWidth(String param) {
        HomePanelWidth = param;
    }

    public void setHomePanelHeight(String param) {
        HomePanelHeight = param;
    }

    public void setHomePanelShadowDepth(int param) {
        HomePanelShadowDepth = param;
    }

    public void setHomePanelShowShadow(Boolean param) {
        HomePanelShowShadow = param;
    }

    public void setHomePanelBGcolor(String param) {
        HomePanelBGcolor = param;
    }

    public Canvas getTabpaneTemplate(Canvas canvasToSet) {
        canvasToSet.setWidth(TabPaneWidth);
        canvasToSet.setHeight(TabPaneHeight);
        canvasToSet.setMargin(TabPaneMargin);
        canvasToSet.setOverflow(TabPaneOverFlow);
        canvasToSet.setBackgroundColor(TabPaneBGcolor);
        return canvasToSet;
    }

    public ListGrid getGridTemplate(ListGrid gridToSet) {
        gridToSet.setWidth(GridWidth);
        gridToSet.setHeight(GridHeight);
        gridToSet.setBackgroundColor(GridBGcolor);
        gridToSet.setAlternateRecordStyles(GridAlternateRecord);
        gridToSet.setShowEmptyMessage(GridShowEmptyMsg);
        gridToSet.setEmptyMessage(GridEmptyMessage);
        gridToSet.setLoadingDataMessage(GridLoadingMsg);
        gridToSet.setShowFilterEditor(GridShowEditor);
        gridToSet.setAutoFetchData(GridAutoFetchData);
        gridToSet.setDataFetchMode(GridDataFetchMode);
        return gridToSet;
    }

    public DynamicForm getFormTemplate(DynamicForm formToSet) {
        formToSet.setBackgroundColor(FormBGcolor);
        formToSet.setCellPadding(FormCellpadding);
        formToSet.setWidth(FormWidth);
        formToSet.setHeight(FormHeight);
        formToSet.setAlign(FormAlign);
        formToSet.setOverflow(Overflow.HIDDEN);
        formToSet.setTitleOrientation(FormTitleOrientation);
        formToSet.setTitleSuffix(FormTitleSuffixe);
        formToSet.setHiliteRequiredFields(FormHiliteRequired);
        formToSet.setRequiredTitlePrefix(FormRTitlePrefix);
        formToSet.setRequiredTitleSuffix(FormRTitleSuffix);
        formToSet.setAutoFetchData(FormAutoFetch);
        return formToSet;
    }

    public Layout getHomePanelTemplate(Layout layoutToSet) {
        layoutToSet.setWidth(HomePanelWidth);
        layoutToSet.setHeight(HomePanelHeight);
        layoutToSet.setShadowDepth(HomePanelShadowDepth);
        layoutToSet.setShowShadow(HomePanelShowShadow);
        layoutToSet.setBackgroundColor(HomePanelBGcolor);

        return layoutToSet;
    }

}
